cmake_minimum_required(VERSION 3.15)

project(
    MyProject
    VERSION 1.0.0
    DESCRIPTION "A project using spdlog, Eigen, PCL, and OpenCV"
    LANGUAGES C CXX
    )

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

if(MSVC)
    add_compile_options(/W4 /permissive-)  # Windows 警告等级
else()
    add_compile_options(-Wall -Wextra -Wpedantic -O2)
endif()


set(SRC_DIR ${PROJECT_SOURCE_DIR}/src)
set(INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)

file(GLOB_RECURSE SRC_FILES ${SRC_DIR}/*.cpp)
# include_directories(${INCLUDE_DIR})

find_package(MPI REQUIRED COMPONENTS C CXX)
find_package(spdlog REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(OpenCV REQUIRED)
# find_package(MPI REQUIRED)
find_package(PCL REQUIRED COMPONENTS common io visualization)


# 方式 1：构建可执行文件
add_executable(MyApp ${SRC_FILES})

# 方式 2：构建静态或动态库
# add_library(${PROJECT_NAME} STATIC ${SRC_FILES})
# add_library(${PROJECT_NAME} SHARED ${SRC_FILES})
target_include_directories(MyApp
    PRIVATE
        ${INCLUDE_DIR}
        ${PCL_INCLUDE_DIRS}
        ${OpenCV_INCLUDE_DIRS}
)


target_link_libraries(
    MyApp
    PRIVATE
    spdlog::spdlog
    Eigen3::Eigen
    MPI::MPI_C
    ${PCL_LIBRARIES}
    ${OpenCV_LIBS}
    )

add_definitions(${PCL_DEFINITIONS})

message(STATUS "Project: ${PROJECT_NAME} v${PROJECT_VERSION}")
message(STATUS "Build Type: ${CMAKE_BUILD_TYPE}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
message(STATUS "PCL include dirs: ${PCL_INCLUDE_DIRS}")
message(STATUS "OpenCV version: ${OpenCV_VERSION}")
message(STATUS "MPI include dir: ${MPI_C_INCLUDE_DIRS}")

# 安装可执行文件到 bin
install(
    TARGETS MyApp
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
    INCLUDES DESTINATION include
)
install(DIRECTORY include/ DESTINATION include)

install(
    DIRECTORY /usr/lib/x86_64-linux-gnu/
    DESTINATION lib
    FILES_MATCHING
        PATTERN "libspdlog.so*"
        PATTERN "libfmt.so*"
        PATTERN "libvtk*.so*"
)

# install(FILES /usr/lib/x86_64-linux-gnu/libspdlog.so.1.12 DESTINATION lib)
# install(FILES /usr/lib/x86_64-linux-gnu/libspdlog.so.1.12.0 DESTINATION lib)

# install(FILES /usr/lib/x86_64-linux-gnu/libfmt.so.9.1.0 DESTINATION lib)
# install(FILES /usr/lib/x86_64-linux-gnu/libfmt.so.9.1.0 DESTINATION lib)
# install(FILES /usr/lib/x86_64-linux-gnu/libvtkCommonCore-9.1.so.9.1.0 DESTINATION lib)


set_target_properties(MyApp PROPERTIES
    INSTALL_RPATH "$ORIGIN/../lib"
    BUILD_WITH_INSTALL_RPATH TRUE
    INSTALL_RPATH_USE_LINK_PATH FALSE
)


set(CPACK_GENERATOR "TGZ")

# 安装依赖库（可选，如果是自定义库）
# install(TARGETS mylib
#     LIBRARY DESTINATION lib
# )

# 安装配置文件或资源文件（可选）
# install(FILES config.yaml DESTINATION share/myapp)

# # ===============================
# # 打包（package 阶段）
# # ===============================

# include(CPack)

# set(CPACK_PACKAGE_NAME "MyApp")
# set(CPACK_PACKAGE_VENDOR "YourCompany")
# set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "My awesome C++ application")
# set(CPACK_PACKAGE_VERSION ${PROJECT_VERSION})
# set(CPACK_PACKAGE_CONTACT "you@example.com")
# set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")

# # 生成 .tar.gz 包
# set(CPACK_GENERATOR "TGZ")
# # 如果你想生成 .deb 包：
# # set(CPACK_GENERATOR "DEB")

include(CPack)